<template>
	<view>
		<view class="headInfo">
			<view class="headimg">
				<image src="../../static/img/head.png" mode="aspectFill"></image>
			</view>

			<view class="userInfo">
				<text class="name" style="margin-bottom: 10rpx;">{{nickname}}</text>
				<picker @change="pickerChange" :value="index" :range="picker">
					<text class="team" @click="readName(teamName)">
						{{teamName}}
					<text class="right lg text-gray cuIcon-right"></text></text>
				</picker>
			</view>
			<!-- <view class="userInfo" v-if="teamArr.length==0">
				<text class="name">{{nickname}}</text>
				<text class="team" @click="readName(teamName)">
						{{teamName}}
				</text>
			</view> -->
			<view class="loginOut" @click="getOut()">
				<image src="../../static/img/basics/u4729.png" mode="" class="out"></image>
				<text>退出</text>
			</view>
			<view class="message" @click="goMessage()">
				<text class="lg text-gray cuIcon-notice messageicon"></text>
				<view class="cu-tag badge num" v-show="message>0">{{message}}</view>
			</view>
		</view>
		<view class="cu-list menu zyq-haspad mylist">
			<view class="cu-item arrow" @click="goShift()">
				<view class="content">
					<image src="../../static/img/basics/u205.png" mode="aspectFill" class="setimg"></image>
					<text class="text-grey">交接班</text>
				</view>
				<view class="action">
					<view class="activenum" v-if="hasUnReadInfoInfo">{{hasUnReadInfoInfo}}</view>
				</view>
			</view>
			<view class="cu-item arrow" @click="gogoods()">
				<view class="content">
					<image src="../../static/wupinfafang.png" mode="aspectFill" class="setimg"></image>
					<text class="text-grey">物品发放</text>
				</view>
				<view class="action" v-if="singNum>0"> 
					<view class="activenum">{{singNum}}</view>
				</view>
			</view>
			<view class="cu-item arrow" @click='goMySchedul()' >
				<view class="content">
					<image src="../../static/paiban.png" mode="aspectFill" class="setimg"></image>
					<text class="text-grey">我的排班</text>
				</view>
			</view>
		</view>
		<view class="cu-list menu zyq-haspad mylist" style="border-top:30rpx solid #F0F0F0;">
			<view class="cu-item">
				<view class="content" @click="assistant">
					<image src="../../static/my/yuyin.png" mode="aspectFill" class="setimg"></image>
					<text class="text-grey">语音助手</text>
				</view>
				<view class="action cu-form-group" style="margin-right: 0;">
					<switch class='red' @change="switchB" :class="switchs?'checked':''" :checked="switchs?true:false" color="#e54d42"></switch>
				</view>
			</view>
			<view class="cu-item arrow" @click="launch">
				<view class="content">
					<image src="../../static/my/qidong.png" mode="aspectFill" class="setimg"></image>
					<text class="text-grey">启动首页</text>
				</view>
				<view class="action">
					<picker @change="fwChange" :value="indexs" :range="pickers">
						<text class="team" style="color: #999999;font-size: 28rpx;">
							{{pickers[indexs]}}
						<text class="right lg text-gray"></text></text>
					</picker>
				</view>
			</view>
			<view class="cu-item">
				<view class="content" @click="version">
					<image src="../../static/my/banben.png" mode="aspectFill" class="setimg"></image>
					<text class="text-grey">版本信息</text>
				</view>
				<view class="action" style="color: #999;">
					<text class="text-sm">V{{versions}}</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				message:'',//消息
				index:-1,//第几个弹框
				indexs:0,//第几个弹框
				picker: [],//班组
				pickers:['首页','服务','我的'],//服务
				teamName:'',//班组名称
				teamArr:[],//班组数组
				nickname:'',//班组名称
				switchs: true,//是否打卡
				hasUnReadInfoInfo:'',//交接班数量
				versions:'',//版本
				teamId:'',//班组id
				singNum:''//物品发放数量
			}
		},
		onLoad() {
		},
		onUnload() {
		},
		onHide() {
			// 移除监听事件
			uni.$off("NFC_READ");
			console.log('NFC_READNFC_READNFC_READNFC_READNFC_READNFC_READNFC_READsss')
		},
		onTabItemTap(){
			if(this.$store.state.state&&this.$store.state.isphone){
				getApp().globalData.TTSSpeaker.stop();
				getApp().globalData.TTSSpeaker.speak(`我的`,1);
			}
			this.getCountNum();
			this.hasUnReadInfo()
			this.getHasReadMessageFlag();
			this.getProperty()
		},
		onShow() { 
			
			this.getTeamByEmployeeId()
			const that = this;
			uni.$on("NFC_READ", function(msg) {
				that.readCard(msg.rfid);
			});
			
			if(uni.getStorageSync('indexs')){
				this.indexs = uni.getStorageSync('indexs')
			}
		
			this.nickname = uni.getStorageSync('nickname')
			 console.log(this.nickname)
			this.picker = []
			this.teamName = uni.getStorageSync('teamName')
			this.waiting();
		},
		methods: {
			// 待发放
			waiting(){
				let param  = {}
				param.teamId = uni.getStorageSync("teamId")
				this.$http("/warehouse-service/outSend/listPageForApp", "get", param).then((res) => {
				    if (res.data.code == 200) {
						this.singNum = res.data.data.total;
					}
				  }
				);
			},
			gogoods(){
				uni.navigateTo({
					url:'goods/list/list'
				})
			},
			// ### 保存用户登录班组
			saveLoginTeam(){
				let param = {}
				param.teamId = this.teamId;
				param.employeeId = uni.getStorageSync('employeeId')
				this.$http('/employee-service/app/saveLoginTeam','post',param).then(res=>{
					if(res.data.code==200){
					}
				});
			},
			// 获取用户所有班组
			getTeamByEmployeeId(){
				let param = {}
				param.employeeId = uni.getStorageSync('employeeId')
				this.$http('/employee-service/app/getTeamByEmployeeId','get',param).then(res=>{
					if(res.data.code==200){
						this.teamArr = []
						this.teamArr = res.data.data
						for(let item of this.teamArr){
							this.picker.push(item.name)
						}
					}
				});
			},
		readCard(id) {
			console.log(9999)
			this.$http('/implement-service/implement/getByRfidCardIdForService', 'get', {
				rfidCardId: id
			}).then(res => {
				console.log(res, 9999)
				if (res.data.code == 200) {
					const data = res.data.data;
					console.log(data)
					if (data == undefined) {
		
					}
					if (data.serviceTarget == 0) {
						let param = {
							id: data.serviceObject.id
						}
						this.$http('/care-service/oldBaseDetail/getOldBaseInfo', 'get', param).then(res => {
							 console.log(res,'resresresres')
							if (!res.data.data.outStatus) {
								if (this.$store.state.state && this.$store.state.isphone) {
									getApp().globalData.TTSSpeaker.stop();
									getApp().globalData.TTSSpeaker.speak('长者已经外出', 1);
								}
								uni.showToast({
									title:'长者已经外出',
									icon:'none'
								})
							} else {
								if (this.$store.state.state && this.$store.state.isphone) {
									getApp().globalData.TTSSpeaker.stop();
									getApp().globalData.TTSSpeaker.speak('进入芯片对应长者详情页面', 1);
								}
								uni.navigateTo({
									url: '/pages/basics/services/detail?oldBaseId=' + res.data.data.oldBaseId
								})
							}
						});
					}
					if (data.serviceTarget == 1) {
						if (this.$store.state.state && this.$store.state.isphone) {
							getApp().globalData.TTSSpeaker.stop();
							getApp().globalData.TTSSpeaker.speak('进入芯片对应房间详情页面', 1);
						}
						uni.navigateTo({
							url: '/pages/basics/daily/detail?roomName=' + data.serviceObject.name + '&roomId=' + data.serviceObject.id
						})
					}
					if (data.serviceTarget == 2) {
						if (this.$store.state.state && this.$store.state.isphone) {
							getApp().globalData.TTSSpeaker.stop();
							getApp().globalData.TTSSpeaker.speak('进入芯片对应服务点详情页面', 1);
						}
						uni.navigateTo({
							url: '/pages/basics/regional/detail?id=' + data.serviceObject.id
						})
					}
				} else if (res.data.code == 2000) {
					if (this.$store.state.state && this.$store.state.isphone) {
						getApp().globalData.TTSSpeaker.stop();
						getApp().globalData.TTSSpeaker.speak(res.data.message, 1);
					}
				}
			})
		},
			getProperty(){
				var that = this
				console.log(9999); 
				if (!navigator) {
					plus.runtime.getProperty(plus.runtime.appid,(wgtinfo)=>{
						console.log(wgtinfo.version,6666);   //版本号
						that.versions = wgtinfo.version
					})
				}
			},
			fwChange(e){
				this.indexs = e.detail.value
				if(this.$store.state.state&&this.$store.state.isphone){
					getApp().globalData.TTSSpeaker.stop();
					getApp().globalData.TTSSpeaker.speak(this.pickers[this.indexs],1);
				}	
				uni.setStorageSync('indexs',this.indexs)
			},
			hasUnReadInfo(){
				let param = {}
				param.teamId = uni.getStorageSync('teamId')
				this.$http('/care-service/handover/hasUnReadInfo','get',param).then(res=>{
					if(res.data.code==200){
						this.hasUnReadInfoInfo = res.data.data;
					}
				});
			},
			launch(){
				if(this.$store.state.state&&this.$store.state.isphone){
					getApp().globalData.TTSSpeaker.stop();
					getApp().globalData.TTSSpeaker.speak('启动首页',1);
				}	
			},
			version(){
				let versions = this.versions;
				if(this.$store.state.state&&this.$store.state.isphone){
					getApp().globalData.TTSSpeaker.stop();
					getApp().globalData.TTSSpeaker.speak(`当前版本${versions}`,1);
				}	
			},
			readName(name){
				if(this.$store.state.state&&this.$store.state.isphone){
					getApp().globalData.TTSSpeaker.stop();
					getApp().globalData.TTSSpeaker.speak(name,1);
				}	
			},			
			// 顶部是否有消息
			getHasReadMessageFlag() {
				let param = {}
				param.teamId = uni.getStorageSync('teamId')
				this.$http('/care-service/myMessage/getHasReadMessageFlag', 'get', param).then(res => {
					if (res.data.code == 200) {
						this.message = res.data.data
					}
				});
			},
			switchB(e) {
				this.switchs = e.detail.value
				if(e.detail.value){
					this.$store.state.state = true
				}else{
					this.$store.state.state = false
				}
				// console.log(e.detail.value,999999);
			},
			// 切换班组
			pickerChange(e) {
				if(this.$store.state.state&&this.$store.state.isphone){
					getApp().globalData.TTSSpeaker.stop();
					getApp().globalData.TTSSpeaker.speak('切换班组',1);
				}	
				this.index = e.detail.value
				this.teamName = this.picker[e.detail.value]
				uni.removeStorageSync('teamId');
				uni.removeStorageSync('teamName');
				this.teamId = this.teamArr[e.detail.value].id
				uni.setStorageSync('teamId',this.teamArr[e.detail.value].id);
				uni.setStorageSync('teamName',this.teamArr[e.detail.value].name);
				this.saveLoginTeam();
			},
			goMessage(){
				if(this.$store.state.state&&this.$store.state.isphone){
					getApp().globalData.TTSSpeaker.stop();
					getApp().globalData.TTSSpeaker.speak(`消息中心`,1);
				}	
				uni.navigateTo({
					url: '/pages/my/message-center/message-center'
				})
			},
			//跳转到交接班
			goShift(){
				if(this.$store.state.state&&this.$store.state.isphone){
					getApp().globalData.TTSSpeaker.stop();
					getApp().globalData.TTSSpeaker.speak('交接班',1);
					uni.navigateTo({
							url: '/pages/my/shift/shift'
					})
				}else{
					uni.navigateTo({
						url: '/pages/my/shift/shift'
					})
				}	
			},
			// 退出登录
			getOut(){
				if(this.$store.state.state&&this.$store.state.isphone){
					getApp().globalData.TTSSpeaker.stop();
					getApp().globalData.TTSSpeaker.speak('退出',1);
						uni.showModal({
						    title: '确认退出',
						    content: '请确认是否退出到登录页面',
						    success: function (res) {
								uni.$off("NFC_READ");
								uni.removeStorageSync('userInfo');
								uni.removeStorageSync('nickname');
								uni.removeStorageSync('employeeId');
								uni.removeStorageSync('teamArr');
								uni.removeStorageSync('teamId');
								uni.removeStorageSync('teamName');
						        if (res.confirm) {
									getApp().globalData.TTSSpeaker.stop();
									getApp().globalData.TTSSpeaker.speak('请确认是否退出到登录页面',1);
										uni.reLaunch({
											url:'../user/login'
										})
						        } else if (res.cancel) {
						            console.log('用户点击取消');
									getApp().globalData.TTSSpeaker.stop();
									getApp().globalData.TTSSpeaker.speak('取消',1);
									
						        }
						    }
						})
				}else{
					uni.showModal({
					    title: '确认退出',
					    content: '确认退出到登录页面？',
					    success: function (res) {
							uni.$off("NFC_READ");
							uni.removeStorageSync('userInfo');
							uni.removeStorageSync('nickname');
							uni.removeStorageSync('employeeId');
							uni.removeStorageSync('teamArr');
							uni.removeStorageSync('teamId');
							uni.removeStorageSync('teamName');
					        if (res.confirm) {
								uni.reLaunch({
									url:'../user/login'
								})
					            console.log('用户点击确定');
					        } else if (res.cancel) {
					            console.log('用户点击取消');
					        }
					    }
					});
				}	
				
			},
			// 跳转到我的排班
			goMySchedul(){
				if(this.$store.state.state&&this.$store.state.isphone){
					getApp().globalData.TTSSpeaker.stop();
					getApp().globalData.TTSSpeaker.speak('我的排班',1);
						uni.navigateTo({
							url: '/pages/my/my-schedul/my-schedul'
						})
				}else{
					uni.navigateTo({
						url: '/pages/my/my-schedul/my-schedul'
					})
				}
			},
			// 获取未读的交接班数量
			getCountNum(){
				this.$http('/care-service/handover/hasUnReadInfo','get',{teamId:uni.getStorageSync('teamId')}).then(res=>{
					console.log(res)
				})
			},
			assistant(){
				if(this.$store.state.state&&this.$store.state.isphone){
					getApp().globalData.TTSSpeaker.stop();
					getApp().globalData.TTSSpeaker.speak('语音助手',1);
				}
			}
		}
	}
</script>

<style lang="scss">
	.headInfo {
		width: 100%;
		height: 300rpx;
		background: rgb(0, 87, 168);
		box-sizing: border-box;
		padding: 100rpx 32rpx 0;
		display: flex;
		align-items: center;
		justify-content: space-between;
		position: relative;

		.headimg {
			width: 100rpx;
			height: 100rpx;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.userInfo {
			flex: 1;
			font-size: 32rpx;
			color: #fff;
			display: flex;
			flex-direction: column;
			margin-left: 30rpx;
			.team {
				margin-top: 12rpx;
				font-size: 26rpx;
				margin-top: 20rpx;
			}
		}

		.loginOut {
			width: auto;
			height: 60rpx;
			border: 1px solid #fff;
			border-radius: 80rpx;
			display: flex;
			align-items: center;
			box-sizing: border-box;
			padding: 0 10rpx;

			image {
				width: 40rpx;
				height: 40rpx;
				margin-right: 20rpx;
			}

			text {
				font-size: 28rpx;
				color: #fff;
			}
		}

		.message {
			position: absolute;
			right: 32rpx;
			bottom: 150rpx;

			.messageicon {
				color: #fff;
				font-size: 40rpx;
			}

			.num {
				position: absolute;
				right: -30rpx;
			}
		}
	}

	.mylist {
		background: #fff;
		.cu-item {
			padding: 0!important;
		}
		.content {
			display: flex;
			align-items: center;
		}
		.action{
			margin-right: 40rpx;
		}
		.activenum{
			position: relative;
			top: 4rpx;
			font-size: 28rpx;
			padding: 0rpx 12rpx;
			height: 36rpx;
			line-height: 40rpx;
			display: inline-block;
			color: #fff;
			background-color: #f00;
			border-radius: 50%;
		}
		.setimg {
			width: 100rpx;
		}
		.uni-switch-input:not([class*="bg-"]){
			background: rgb(0, 122, 255)!important;
		}
	}

	.cu-list.menu>.cu-item.arrow:before {
		right: 0;
	}
	.cu-list {
		margin-top: 0;
	}
	
</style>
